package com.py.pythink.service.impl;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.py.pythink.config.RedisKeyConfig;
import com.py.pythink.config.SystemConfig;
import com.py.pythink.dao.OssLogDao;
import com.py.pythink.dao.UserDetailDao;
import com.py.pythink.entity.OssLog;
import com.py.pythink.entity.User;
import com.py.pythink.entity.UserDetail;
import com.py.pythink.service.intf.OssLogService;
import com.py.pythink.third.AliOssUtil;
import com.py.pythink.third.JedisUtil;
import com.py.pythink.util.DataUtil;
import com.py.pythink.util.FileUtil;
import com.py.pythink.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.io.IOException;
import java.util.Date;

@Service
public class OssLogServiceImpl implements OssLogService {
    @Autowired
    private OssLogDao dao;
    @Autowired
    private UserDetailDao detailDao;
    //上传
    @Override
    public R upload(MultipartFile file, String token) {//文件上传，上传的文件
        if(JedisUtil.checkKey(RedisKeyConfig.USER_TOKEN+token)){//检查用户是否处于登陆状态
            if(!file.isEmpty()){//检验文件是否为空
                String rename = FileUtil.rename(file.getOriginalFilename());//文件的重命名
                Date date = DataUtil.getDate(SystemConfig.OSS_URL_DAYS);//获取有效期
                System.out.println(date);
                try {
                    String url = AliOssUtil.upload(rename, date, file.getInputStream());//调用第三方接口
                    if(StringUtils.hasLength(url)){//判断url
                        //新增上传日志
                        OssLog ossLog = new OssLog(SystemConfig.OSS_1, AliOssUtil.bucket, rename, url, date, new Date());
                        if(dao.insert(ossLog)>0){
                            return R.ok(ossLog);
                        }

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return R.fail();
    }

    @Override
    @Transactional
    public R uploadImg(MultipartFile img, String token) {
        if(JedisUtil.checkKey(RedisKeyConfig.USER_TOKEN+token)){
            if(!img.isEmpty()){
                String rename = FileUtil.rename(img.getOriginalFilename());//文件的重命名
                System.out.println(rename);
                Date date = DataUtil.getDate(SystemConfig.OSS_URL_DAYS);
                System.out.println(date);
                try {
                    String url = AliOssUtil.upload(rename, date, img.getInputStream());
                    if(StringUtils.hasLength(url)){
                        OssLog ossLog = new OssLog(SystemConfig.OSS_1, AliOssUtil.bucket, rename, url, date, new Date());
                        if(dao.insert(ossLog)>0){
                            User user = (User)JSONArray.parseObject(JedisUtil.getStr(RedisKeyConfig.USER_TOKEN + token),User.class);
                            detailDao.updateByUid(ossLog.getId(),user.getId());
                            return R.ok(ossLog);
                        }

                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return R.fail();
    }

    @Override
    public R all() {
       return R.ok(dao.selectList(null));
    }
}
